﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Entropy.Ext.Ast;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Entropy.Ext.Test
{
	[TestClass]
	public class SemanticParserTest
	{

		[TestMethod]
		public void TestSimpleString()
		{
			const string simple = "simple";
			Expression<Func<object, string, object[], int, int, IStore, IView, string>> renderCompany =
				(value, metaData, record, rowIndex, colIndex, store, view) => simple;

			var result = SemanticParser.ParseExpressions(new[] {renderCompany}, null, new Dictionary<Node, string>());
			Assert.IsInstanceOfType(result, typeof (FunctionDefinition));
			var def = (FunctionDefinition) result[0];
			Assert.IsInstanceOfType(def.Root, typeof(Const));
			var const_ = (Const) def.Root;
			Assert.AreEqual(const_.Value, "'" + simple + "'");

		}

		[TestMethod]
		public void TestQuestionOperator()
		{
			const string sIfTrue = "true";
			const string sIfFalse = "false";

			Expression<Func<object, string, object[], int, int, IStore, IView, string>> renderCompany =
				(value, metaData, record, rowIndex, colIndex, store, view) => value != null ? sIfTrue : sIfFalse;        

			var result = SemanticParser.ParseExpressions(new[] {renderCompany}, null, new Dictionary<Node, string>());
			Assert.IsInstanceOfType(result, typeof (FunctionDefinition));
			var def = (FunctionDefinition) result[0];
			Assert.IsInstanceOfType(def.Root, typeof(Conditional));
			var cond = (Conditional) def.Root;
			var test = (Binary) cond.Test;
			var param = (Param) test.Left;
			Assert.AreEqual(param.Name, "value");
			var nil = (Const)test.Right;
			Assert.AreEqual(nil.Value, "null");

			var ifTrue = (Const) cond.IfTrue;
			Assert.AreEqual(ifTrue.Value, "'" + sIfTrue + "'");
			var const_ = (Const) cond.IfFalse;
			Assert.AreEqual(const_.Value, "'" + sIfFalse + "'");

		}

		[TestMethod]
        public void TestPropertySet()
        {
            const string simple = "simple";
            Expression<Func<object, string, object[], int, int, IStore, IView, string>> renderCompany =
                (value, metaData, record, rowIndex, colIndex, store, view) => simple;

            var result = SemanticParser.ParseExpressions(new[] {renderCompany}, null, new Dictionary<Node, string>());
            Assert.IsInstanceOfType(result, typeof(FunctionDefinition));
            var def = (FunctionDefinition)result[0];
            Assert.IsInstanceOfType(def.Root, typeof(Const));
            var const_ = (Const)def.Root;
            Assert.AreEqual(const_.Value, "'" + simple + "'");

        }


	}
}
